﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Transactions;
using System.Text;
using System.Web.Services;
using System.Collections;
using Catering_Model;
using Catering.View_Operaction;


namespace Catering
{
    public partial class new_peic : System.Web.UI.Page
    {
        View_Operaction_Common voc = new View_Operaction_Common();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["User"] != null)
                {
                    string userid = Session["PersonnelID"].ToString();
                    int companyid = 0;
                    int groupid = 0;
                    int classid = 0;
                    bool isAdmin = false;
                    Bind_Data_to_Controls.GetCompanyByCurrentUser(userid, out companyid, out groupid, out classid, out isAdmin);

                    Bind_Data_to_Controls.Bind_Area_ddl(Dr_Pic_Group, false, groupid, companyid);
                    Bind_Data_to_Controls.Bind_Class_ddl(Dr_Pic_Group.SelectedItem, Dr_Pic_Class, false, classid);
                }
                voc.DropDownListDataBind_Group_tedian(dr_r_type);
                ViewState["DishList"] = voc.UITreeViewDataBind_DishTypes("1");
                voc.DropDownListDataBind_Group_tedian(Dr_zuoye);

                this.LoadData();
            }
        }

        //加载页面数据
        public void LoadData()
        {

            this.Drop_Sex.LoadRad(new Catering_Rules.Sex().GetAllList().Tables[0], "id", "SexName");
            this.Drop_Sex.Items[0].Selected = true;

            this.Drop_Age.LoadDrop(new Catering_Rules.OldArea().GetAllList().Tables[0], "id", "Area");

            this.rd_laodong.LoadRad(new Catering_Rules.LaborGrade().GetAllList().Tables[0], "id", "LGName");
            this.rd_laodong.Items[0].Selected = true;
        }

        protected void DrGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            voc.DropDownListDataBind_Class(Dr_Pic_Class, this.Dr_Pic_Group.SelectedValue);
        }

        // 群体
        protected void Group_Next_But_Click(object sender, EventArgs e)
        {
            //所选餐别
            string MealType = string.Empty;
            foreach (ListItem lb in CL_Pic_FoodMealType.Items)
            {
                if (lb.Selected)
                {
                    MealType = MealType + lb.Text + ",";
                }
            }
            //保存群体配餐信息
            Catering_Model.GroupCatering md_GC = new Catering_Model.GroupCatering();
            int GroupID = new Catering_Rules.GroupCatering().GetMaxId();
            md_GC.AreaID = Convert.ToInt32(Dr_Pic_Group.SelectedValue);
            md_GC.ClaassID = Convert.ToInt32(Dr_Pic_Class.SelectedValue);
            md_GC.GCName = Txt_Pic_name.Text;
            md_GC.MealType = MealType;
            md_GC.Characteristics = Txt_Pic_Te.Text;
            md_GC.temp2 = Dr_zuoye.SelectedValue;
            md_GC.id = GroupID;
            new Catering_Rules.GroupCatering().Add(md_GC);

            //保存配餐详细信息
            foreach (DataRow row in dt_PJ.Rows)
            {
                Catering_Model.CateringGroupInfo md = new Catering_Model.CateringGroupInfo();
                md.SexID = Convert.ToInt32(row["SexID"]);
                md.OldAreaID = Convert.ToInt32(row["OldAreaID"]);
                md.LaborGradeID = Convert.ToInt32(row["LaborGradeID"]);
                md.Group_tedianID = Convert.ToInt32(row["Group_tedianID"]);
                md.Number = Convert.ToInt32(row["Number"]);
                md.GroupID = GroupID;
                md.id = new Catering_Rules.CateringGroupInfo().GetMaxId();
                new Catering_Rules.CateringGroupInfo().Add(md);
            }
            Response.Redirect(String.Format("select_Date.aspx?cid={0}&IsGroupCatering=true", GroupID));
        }

        //个人
        protected void Sigen_Next_But_Click(object sender, EventArgs e)
        {
            Catering_Rules.PersonalService crps = new Catering_Rules.PersonalService();
            Catering_Model.PersonalService md = new Catering_Model.PersonalService();
            md.id = crps.GetMaxId();
            md.CateringName = this.TbSingeName.Text.Trim();
            md.OldArea = Convert.ToInt32(this.Drop_Age.SelectedValue);
            md.sex = this.Drop_Sex.SelectedValue;
            md.Hight = this.tb_hegth.Text.Trim();
            md.Weight = this.tb_weight.Text.Trim();
            md.LaborGradeID = Convert.ToInt32(this.rd_laodong.SelectedValue);
            md.MealTypeID = Convert.ToInt32(dr_shanshi.SelectedValue);
            md.MealType = ckcanbie.SelectedItem.Text;//未起作用 有误
            md.temp2 = this.dr_r_type.SelectedValue;//适用人群 特点
            crps.Add(md);

            //保存个人详细信息
            Catering_Model.CateringGroupInfo md_info = new Catering_Model.CateringGroupInfo();
            md_info.SexID = Convert.ToInt32(this.Drop_Sex.SelectedValue);
            md_info.OldAreaID = Convert.ToInt32(this.Drop_Age.SelectedValue);
            md_info.LaborGradeID = Convert.ToInt32(this.rd_laodong.SelectedValue);
            md_info.Group_tedianID = Convert.ToInt32(this.dr_r_type.SelectedValue);
            md_info.Number = 1;
            md_info.temp1 = md.id.ToString();//保存个人配餐ID
            md_info.id = new Catering_Rules.CateringGroupInfo().GetMaxId();
            new Catering_Rules.CateringGroupInfo().Add(md_info);

            Response.Redirect(String.Format("select_Date.aspx?cid={0}", md.id.ToString()));

        }

        //获取年龄option
        public string GetAgeOption()
        {
            DataTable dt_OldArea = new Catering_Rules.OldArea().GetAllList().Tables[0];
            return dt_OldArea.GetOption("id", "Area");
        }

        //获取性别option
        public string GetSexOption()
        {
            DataTable dt_Sex = new Catering_Rules.Sex().GetAllList().Tables[0];
            return dt_Sex.GetOption("id", "SexName");
        }

        //获取劳动级别
        public string GelLaborGradeOption()
        {
            DataTable dt_LaborGrade = new Catering_Rules.LaborGrade().GetAllList().Tables[0];
            return dt_LaborGrade.GetOption("id", "LGName");
        }

        //获取加权品均值
        [WebMethod]
        public static ArrayList GetJiaQuanPJ(string strTypes)
        {

            ArrayList al = new ArrayList();
            string[] s_Types = strTypes.TrimEnd(',').Split(',');//获取人群各个类别
            decimal[] bl_Num = new decimal[24]; //存放最后的标量值
            ArrayList No_repeatTypes = new ArrayList();//存放非重复的类别
            StringBuilder sb = new StringBuilder();//标量值查询条件
            sb.Append("1=2");

            Load_dtPJ();
            //获取不重复的配餐类别组合
            foreach (string str in s_Types)
            {
                Add_dtPJ(str);
                string[] s_Num = str.Split('|');
                string s_str = s_Num[0] + "|" + s_Num[1] + "|" + s_Num[2] + "|" + s_Num[3];//获取类别排除人数
                if (!No_repeatTypes.Contains(s_str))
                {
                    No_repeatTypes.Add(s_str);
                }
            }

            //获取标量值条件
            foreach (string str in No_repeatTypes)
            {
                string Group_tedianId = str.Split('|')[0]; //适合人群
                string SexId = str.Split('|')[1];//性别
                string AgeId = str.Split('|')[2];//年龄段
                string LaborGradeId = str.Split('|')[3];//劳动级别
                string strwhere = "SexID=" + SexId + " and OldAreaID=" + AgeId + " and LaborGradeID=" + LaborGradeId + " and Group_tedianID=" + Group_tedianId;

                DataTable dt_IngestionIsExists = new Catering_Rules.Ingestion().GetList(strwhere).Tables[0];
                if (dt_IngestionIsExists != null && dt_IngestionIsExists.Rows.Count > 0)
                {
                    sb.Append(" or (" + strwhere + ")");
                }
                else //存在无摄入量标准的类别组合
                {
                    al.Add("NO");
                    return al;
                }
            }

            //计算标量值
            DataTable dt_Ingestion = new Catering_Rules.Ingestion().GetList(sb.ToString()).Tables[0];
            foreach (DataRow row in dt_Ingestion.Rows)
            {
                bl_Num[Convert.ToInt32(row["NutritionElementsID"]) - 1] += Convert.ToDecimal(row["Number"]);
            }

            al.Add("OK");
            //填充返回列表
            al.Add((Convert.ToDouble(bl_Num[0]) / No_repeatTypes.Count).ToString());
            for (int i = 0; i < 24; i++)
            {
                if (i == 0)
                {
                    al.Add((Convert.ToDouble(bl_Num[i]) / No_repeatTypes.Count * 4.182).ToString());
                }
                else
                {
                    al.Add((Convert.ToDouble(bl_Num[i]) / No_repeatTypes.Count).ToString());
                }
            }
            return al;
        }

        //加载群体配餐详细DT
        public static DataTable dt_PJ;
        public static void Load_dtPJ()
        {
            dt_PJ = new DataTable();
            dt_PJ.Columns.Add("SexID");
            dt_PJ.Columns.Add("OldAreaID");
            dt_PJ.Columns.Add("LaborGradeID");
            dt_PJ.Columns.Add("Group_tedianID");
            dt_PJ.Columns.Add("Number");
        }

        //填充群体配餐DT
        public static void Add_dtPJ(string strTypes)
        {
            string Group_tedianId = strTypes.Split('|')[0]; //适合人群
            string SexId = strTypes.Split('|')[1];//性别
            string AgeId = strTypes.Split('|')[2];//年龄段
            string LaborGradeId = strTypes.Split('|')[3];//劳动级别
            string Number = strTypes.Split('|')[4];//劳动级别

            DataRow row = dt_PJ.NewRow();
            row["SexID"] = SexId;
            row["OldAreaID"] = AgeId;
            row["LaborGradeID"] = LaborGradeId;
            row["Group_tedianID"] = Group_tedianId;
            row["Number"] = Number;
            dt_PJ.Rows.Add(row);

        }

        //获取加权品均值 个人
        [WebMethod]
        public static ArrayList GetJiaQuanPJByGeRen(string strTypes)
        {
            ArrayList al = new ArrayList();
            decimal[] bl_Num = new decimal[24]; //存放标量值

            //获取标量值条件
            string Group_tedianId = strTypes.Split('|')[0]; //适合人群
            string SexId = strTypes.Split('|')[1];//性别
            string AgeId = strTypes.Split('|')[2];//年龄段
            string LaborGradeId = strTypes.Split('|')[3];//劳动级别
            string strwhere = "SexID=" + SexId + " and OldAreaID=" + AgeId + " and LaborGradeID=" + LaborGradeId + " and Group_tedianID=" + Group_tedianId;

            DataTable dt_Ingestion = new Catering_Rules.Ingestion().GetList(strwhere).Tables[0];
            if (dt_Ingestion != null && dt_Ingestion.Rows.Count > 0)
            {
                foreach (DataRow row in dt_Ingestion.Rows)
                {
                    bl_Num[Convert.ToInt32(row["NutritionElementsID"]) - 1] = Convert.ToDecimal(row["Number"]);
                }
                al.Add("OK");
                al.Add((Convert.ToDecimal(bl_Num[0])).ToString());
                for (int i = 0; i < 24; i++)
                {
                    al.Add((Convert.ToDecimal(bl_Num[i])).ToString());
                }
            }
            else //存在无摄入量标准的类别组合
            {
                al.Add("NO");
            }

            return al;
        }

        [WebMethod]
        public static ArrayList GetShuoMingStr(int id)
        {
            ArrayList al = new ArrayList();
            Catering_Rules.Group_tedian operation = new Catering_Rules.Group_tedian();
            Group_tedian gt = operation.GetModel(id);
            al.Add(string.IsNullOrEmpty(gt.temp1) ? "" : gt.temp1);
            return al;
        }
    }
}